/*
 * Powered By [rapid-framework]
 * Web Site: http://www.rapid-framework.org.cn
 * Google Code: http://code.google.com/p/rapid-framework/
 * Since 2008 - 2012
 */

package com.tudou.searchcontrol.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.codehaus.jackson.JsonParseException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.tudou.searchcontrol.model.Album;
import com.tudou.searchcontrol.model.PageInfo;
import com.tudou.searchcontrol.model.Playsource;
import com.tudou.searchcontrol.service.IAlbumService;
import com.tudou.searchcontrol.service.IPlaysourceService;



@Controller
@RequestMapping("/album/*")
public class AlbumAction extends BaseAction{

	@Autowired
	private IAlbumService albumService;
	
	@Autowired
	private IPlaysourceService playsourceService;
	
	@RequestMapping("list")
	public String list(HttpServletRequest request, HttpServletResponse response, ModelMap model,Album album,PageInfo pageInfo){
		Map searchMap = new HashMap();
		pageInfo =albumService.getAlbumByPage(searchMap,pageInfo);
		
		model.put("PageInfo", pageInfo);
		model.put("searchMap", searchMap);

		return "/album/albumList";
	}
	
//	@RequestMapping("list")
//	public String list(HttpServletRequest request, HttpServletResponse response, ModelMap model,Album album,PageInfo pageInfo){
//		Map searchMap = new HashMap();
//		pageInfo =albumService.getAlbumByPage(searchMap,pageInfo);
//		
//		model.put("dataList", pageInfo.getDataList());
//		model.put("searchMap", searchMap);
//
//		return "/album/albumList";
//	}
	
	@RequestMapping("view")
	public String view(HttpServletRequest request, HttpServletResponse response, ModelMap model,Long id){
		Album album = albumService.getAlbumById(id);
		model.put("machineSetting", album);
		return "/album/albumView";
	}
	
	
	@RequestMapping("edit")
	public String edit(HttpServletRequest request, HttpServletResponse response, ModelMap model,Album album){
		if(album.getId() != null){
			album = albumService.getAlbumById(album.getId());
			
			Map searchMap = new HashMap();
			searchMap.put("albumId", album.getId());
			List<Playsource> playsources = playsourceService.getPlaysourceList(searchMap);
			model.put("playsources", playsources);
			
		}

		
		model.put("album", album);
		return "/album/albumEdit";
	}
	
	
	@RequestMapping("playVideo")
	public String play(HttpServletRequest request, HttpServletResponse response, ModelMap model,Long id,Integer sourcetype){
		
		Map searchMap = new HashMap();
		searchMap.put("vdeoId", id);
		searchMap.put("sourcetype", sourcetype);
		Playsource playsource = playsourceService.getPlaysource(searchMap);
		String playUrl = playsource.getSwfUrl();
		if(playsource.getSourcetype() == 1 && playUrl.indexOf("autoPlay=true")<0){
			playUrl = playUrl.substring(0,playUrl.lastIndexOf("/"))+"/&autoPlay=true"+playUrl.substring(playUrl.lastIndexOf("/"));
		}
		
		model.put("playUrl",playUrl);
		return "/demo/paly";
	}
	@RequestMapping("save")
	public String save(HttpServletRequest request, HttpServletResponse response, ModelMap model,Album album,PageInfo pageInfo){
		int i = albumService.saveOrUpdateAlbum(album);
		
		Album newAlbum = new Album();
		
		return this.list(request, response, model, newAlbum,pageInfo);
	}
	
	
	@RequestMapping("delete")
	public String delete(HttpServletRequest request, HttpServletResponse response, ModelMap model,Album album,String delIds,PageInfo pageInfo){
		if(StringUtils.isEmpty(delIds)){
			delIds = album.getId().toString();
		}
		int i = albumService.deleteAlbum(delIds);
		
		Album newAlbum = new Album();
		return this.list(request, response, model, newAlbum,pageInfo);
	}
}